// pages/cun/cun.js

 /*
 * 二开版本 （由于接口貌似PHP所以在此开发了一套Java后台）
 * Author: 郭少
 * gitee: https://gitee.com/guoshao99/tuodan-manghe?_from=gitee_search
 * QQ：2549789059
 * 微信：2549789059
 */

import {
  areaList
} from '@vant/area-data';
import {
  pay,
  save
} from '../request/wx';
let videoAd = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    openid: wx.getStorageSync('openid'),
    age: '',
    columns: ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'],
    school: '',
    interest:'',
    showArea: false,
    wxaccount: '',
    showXz: false,
    city: '',
    xingzuo: '',
    sex: 0,
    shenhe:2,
    status:2
  },
  radioChange(e) {
    this.setData({
      sex: e.detail.value
    })
  },
  onChange(e) {
    this.setData({
      xingzuo: e.detail.value,
      showXz: false
    })
  },
  closeXz() {
    this.setData({
      showXz: false
    })
  },
  showCity() {
    this.setData({
      showArea: true
    })
  },
  showXz() {
    this.setData({
      showXz: true
    })
  },
  areaConfig(e) {
    console.log(e);
    let value = e.detail.values
    let add = ''
    value.map(v => {
      add += v.name + ' '
    })
    this.setData({
      city: add,
      showArea: false
    })
  },
  closeArea() {
    console.log(1);
    this.setData({
      showArea: false
    })
  },
  submit() {
    let that = this;
    
    let {
      age,
      interest,
      wxaccount,
      xingzuo,
      city,
      sex,
      openid,
      status
    } = this.data
    if (!age) {
      wx.showToast({
        title: '请填写年龄',
        icon: 'none'
      })
      return
    }
    if (!interest) {
      wx.showToast({
        title: '请填写兴趣',
        icon: 'none'
      })
      return
    }
    if (!wxaccount) {
      wx.showToast({
        title: '请填写微信号',
        icon: 'none'
      })
      return
    }
    if (!city) {
      wx.showToast({
        title: '请选择城市',
        icon: 'none'
      })
      return
    }
    if (!xingzuo) {
      wx.showToast({
        title: '请选择星座',
        icon: 'none'
      })
      return
    }
    if (!sex) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return
    }

    let config = wx.getStorageSync('config')
    if (config.isPay == 1) {
      let openid = wx.getStorageSync('openid')
      let type = 1;
      pay({
        openid,
        type
      }).then(pay => {
        wx.requestPayment({
          timeStamp: pay.timeStamp,
          nonceStr: pay.nonceStr,
          package: pay.packageValue,
          signType: pay.signType,
          paySign: pay.paySign,
          success: function (res) {
            save({
              age: Number(age),
              interest,
              wxaccount,
              xingzuo,
              city,
              sex,
              openid: openid,
              status: that.data.status
            }).then(res => {
              console.log(res);
              if(res.code == 200){
                wx.showToast({
                  title: '添加成功',
                })
              }else{
                wx.showToast({
                  title: '未知异常',
                })
              }
              that.setData({
                age: '',
                interest: '',
                wxaccount: '',
                xingzuo: '',
                city: '',
                status: 2
              })
            })
          },
          fail(err) {
            wx.showToast({
              title: '支付失败',
              icon: 'none'
            })
          }
        });
      })
    } else if(config.isPay == 2){
      if (videoAd) {
        videoAd.onClose(close => {
          console.log(close, '关闭');
          // 用户点击了【关闭广告】按钮
          if (close && close.isEnded) {
            save({
              age: Number(age),
              interest,
              wxaccount,
              xingzuo,
              city,
              sex,
              openid: openid,
              status: that.data.status
            }).then(res => {
              console.log(res);
              if(res.code == 200){
                wx.showToast({
                  title: '添加成功',
                })
              }else{
                wx.showToast({
                  title: '未知异常',
                })
              }
              that.setData({
                age: '',
                interest: '',
                wxaccount: '',
                xingzuo: '',
                city: '',
                status: 2
              })
            })

            // 正常播放结束，可以下发游戏奖励
          } else {
            wx.showToast({
              title: '退出广告,保存失败',
              icon: 'none'
            })
            // 播放中途退出，不下发游戏奖励
          }
        })
        videoAd.show().catch(() => {
          // 失败重试
          videoAd.load()
            .then(() => videoAd.show())
            .catch(err => {
              console.log('激励视频 广告显示失败')
            })
        })
      }
    }else{
      save({
        age: Number(age),
        interest,
        wxaccount,
        xingzuo,
        city,
        sex,
        openid: openid,
        status: that.data.status
      }).then(res => {
        console.log(res);
        if(res.code == 200){
          wx.showToast({
            title: '添加成功',
          })
        }else{
          wx.showToast({
            title: '未知异常',
          })
        }
        that.setData({
          age: '',
          interest: '',
          wxaccount: '',
          xingzuo: '',
          city: '',
          status: 2
        })
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let config = wx.getStorageSync('config')
    if (config.isPay == 0) {
      if (wx.createRewardedVideoAd) {
        videoAd = wx.createRewardedVideoAd({
          adUnitId: config.cunid
        })
        videoAd.onLoad(() => {
          console.log('激励视频 广告加载成功')
        })
        videoAd.onError((err) => {
          console.log(err)
        })
      }
    }
	
    this.setData({
      shenhe: config.shenhe,
      status: config.noteShenhe
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})